package cn.git.demo.controller.order;



import cn.git.demo.pojo.Order;
import cn.git.demo.service.OrderService;
import cn.itcast.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.jws.WebParam;

@Controller
@Transactional
public class OrderController {


    @Autowired
    private OrderService orderService;
    @RequestMapping(value = "order/list.do")
    public String list(Long id,Integer paymentWay,Integer isPay,Integer orderState,Integer pageNo,Integer pageSize,Model model){
        System.out.print(id);
        Pagination pagination = orderService.selectPaginationByQuery(id, paymentWay, isPay, orderState, pageNo, pageSize);

        //将查询数据添加到model
        model.addAttribute("pagination",pagination);
        model.addAttribute("id",id);

        if (paymentWay!=null){
            model.addAttribute("paymentWay",paymentWay);
        }else {
            model.addAttribute("paymentWay",null);
        }
        if (isPay!=null){
            model.addAttribute("isPay",isPay);
        }else {
            model.addAttribute("isPay",null);
        }
        if (orderState!=null){
            model.addAttribute("orderState",orderState);
        }else {
            model.addAttribute("orderState",null);
        }

        return "order/list";
    }

    @RequestMapping(value = "/order/view.do")
    public String view(Long id,Integer pageNo,Model model){

        Order order = orderService.selectOrderUserById(id);
        Order order1 = orderService.selectDetailById(id);
        model.addAttribute("order",order);
        model.addAttribute("order1",order1);
        model.addAttribute("pageNo",pageNo);
        return "order/view";
    }
    @RequestMapping(value = "/order/toAdd.do")
    public String edit(Model model){

        return "order/add";
    }
    @RequestMapping(value = "/order/add.do")
    public String add(Order order,String proId){

        System.out.print(proId);
        String[] proId1 = proId.split("/?");
        System.out.print(proId1.toString());
        orderService.addSingleOrder(order);
        return "redirect:list.do";
    }
    @RequestMapping(value = "/order/toEdit.do")
    public String toEdit(Long id,Integer pageNo, Model model){

        Order order = orderService.selectOrderUserById(id);

        model.addAttribute("order",order);

        model.addAttribute("pageNo",pageNo);

        return "order/edit";
    }
    @RequestMapping(value = "/order/edit.do")
    public String edit(Order order,Integer pageNo,RedirectAttributes attributes){

        orderService.updateOrder(order);
        attributes.addAttribute("pageNo",pageNo);

            /*跳转并携带参数的两种方式
            1.手动拼接url  "redirect:list.do?pageNo="+pageNo+""

            2.利用 RedirectAttributes attributes
            形参中加入RedirectAttributes attributes
            利用attributes的addAttribute方法，将需要传入的参数添加进去。
            */
        return "redirect:list.do";
    }
    @RequestMapping(value = "/order/delete.do")
    public String delete(Long id,Integer pageNo,RedirectAttributes attributes){

        orderService.deleteOrderById(id);

        attributes.addAttribute("pageNo",pageNo);

        return "redirect:list.do";
    }
    @RequestMapping(value="/order/order_view.do")
    public String order_view(Long id,Integer pageNo,Model model){

        model.addAttribute("id",id);
        model.addAttribute("pageNo",pageNo);
        return "frame/order_view";
    }
    @RequestMapping(value="/order/createOrderId.do")
    public @ResponseBody String createOrderId(Long id){
        Integer flag = null;
        Order order = orderService.selectOrderUserById(id);
        if(order==null){
            flag = 0;
        }else{
            flag = 1;
        }
        return flag.toString();
    }
    @RequestMapping(value = "/order/deleteAll.do")
    public String delete(Long[] ids){

        return "redirect:list.do";
    }
    @RequestMapping(value = "/order/createOrder.do")
    public String createOrder(Long[] ids,String[] names,Long id,Integer[] pno,Order order,RedirectAttributes attributes){

        orderService.addSingleOrder(order);

        attributes.addAttribute("ids",ids);
        attributes.addAttribute("names",names);
        attributes.addAttribute("id",id);
        attributes.addAttribute("pno",pno);

        return "redirect:/orderDetail/insert.do";
    }



}
